package com.bjsxt.shop.head.controller ; 

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.shop.common.controller.BaseController;
import com.bjsxt.shop.common.util.ConstatFinalUtil;
import com.bjsxt.shop.common.util.PageInfoUtil;
import com.bjsxt.shop.users.pojo.AUsers;
import com.bjsxt.shop.users.pojo.AUsersAddress;
import com.bjsxt.shop.users.service.IUsersService;

/**
 * 管理员登陆后的Controller
 * @author wangshSxt
 *
 */
@Controller
@RequestMapping("/head/users")
public class UsersHeadController extends BaseController
{
	@Resource
	private IUsersService usersService;
	
	/**
	 * 登陆后首页
	 * @return
	 */
	@RequestMapping("/main")
	public String main()
	{
		ConstatFinalUtil.LOGGER.info("---- main ----");
		return "/head/main";
	}
	
	/**
	 * 地址列表
	 * @return
	 */
	@RequestMapping("/addressList")
	public String addressList(HttpServletRequest request)
	{
		ConstatFinalUtil.LOGGER.info("----addressList----");
		HttpSession session = request.getSession() ;
		AUsers users = (AUsers) session.getAttribute("users");
		/* 查询当前用户的所有地址 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		Map<String, Object> condMap = new HashMap<String,Object>();
		condMap.put("usersId", users.getId());
		List<AUsersAddress> usersAddressList = this.usersService.findCondListUsersAddressService(pageInfoUtil, condMap);
		
		request.setAttribute("usersAddressList", usersAddressList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		return "/head/users/addressList" ;
	}
	
	/**
	 * 地址添加
	 * @return
	 */
	@RequestMapping("/addressInsert")
	public String addressInsert(HttpServletRequest request)
	{
		ConstatFinalUtil.LOGGER.info("----addressList----");
		return "/head/users/addressInsert" ;
	}
	
	/**
	 * 地址添加提交
	 * @return
	 */
	@RequestMapping("/addressInsertSubmit")
	public String addressInsertSubmit(HttpServletRequest request,AUsersAddress usersAddress)
	{
		ConstatFinalUtil.LOGGER.info("----addressInsertSubmit----");
		HttpSession session = request.getSession() ;
		AUsers users = (AUsers) session.getAttribute("users");
		usersAddress.setUsersId(users.getId());
		usersAddress.setCreateTime(new Date());
		usersAddress.setUpdateTime(new Date());
		usersAddress.setPubTime(new Date());
		
		JSONObject resultJSON = this.usersService.saveOneUsersAddressService(usersAddress);
		this.info = resultJSON.get("info") + "";
		request.setAttribute("info", info);
		return this.addressInsert(request) ;
	}
	
	/**
	 * 地址添加
	 * @return
	 */
	@RequestMapping("/addressUpdate")
	public String addressUpdate(HttpServletRequest request)
	{
		ConstatFinalUtil.LOGGER.info("----addressList----");
		/* 按照Id查询用户地址 */
		String uaid = request.getParameter("uaid");
		Map<String, Object> condMap = new HashMap<String,Object>();
		condMap.put("id", uaid);
		AUsersAddress usersAddress = this.usersService.findOneUsersAddressService(condMap);
		request.setAttribute("usersAddress", usersAddress);
		return "/head/users/addressUpdate" ;
	}
	
	/**
	 * 地址添加提交
	 * @return
	 */
	@RequestMapping("/addressUpdateSubmit")
	public String addressUpdateSubmit(HttpServletRequest request)
	{
		ConstatFinalUtil.LOGGER.info("----addressInsertSubmit----");
		String uaid = request.getParameter("uaid");
		Map<String, Object> condMap = new HashMap<String,Object>();
		condMap.put("id", uaid);
		AUsersAddress usersAddress = this.usersService.findOneUsersAddressService(condMap);
		request.setAttribute("usersAddress", usersAddress);
		
		String regionId = request.getParameter("regionId");
		String name = request.getParameter("name");
		String contact = request.getParameter("contact");
		String phone = request.getParameter("phone");
		String address = request.getParameter("address");
		String zipCode = request.getParameter("zipCode");
		String isDef = request.getParameter("isDef");
		String status = request.getParameter("status");
		
		usersAddress.setRegionId(Integer.valueOf(regionId));
		usersAddress.setName(name);
		usersAddress.setContact(contact);
		usersAddress.setPhone(phone);
		usersAddress.setAddress(address);
		usersAddress.setZipCode(zipCode);
		usersAddress.setIsDef(Byte.valueOf(isDef));
		usersAddress.setStatus(Byte.valueOf(status));
		usersAddress.setAddress(address);
		usersAddress.setUpdateTime(new Date());
		
		JSONObject resultJSON = this.usersService.updateOneUsersAddressService(usersAddress);
		this.info = resultJSON.get("info") + "";
		request.setAttribute("info", info);
		return this.addressUpdate(request) ;
	}
	
	/**
	 * 退出
	 * @param request
	 * @param session
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping("/logout")
	public String logout(HttpServletRequest request,HttpSession session,HttpServletResponse response) throws IOException
	{
		AUsers users = (AUsers) session.getAttribute("users");
		/* 处理当前服务的session */
		session.removeAttribute("users");
		
		/* 还得操作用户中心 */
		/* 如何客户端跳转到用户中心退出的URL
		 * 如何在用户中心操作token
		 *  */
		
		/* 跳转到用户中心的登陆页面,并且将returnUrl带上去 */
		String usersCenterLogoutUrl = ConstatFinalUtil.BUNDLE.getString("users.center.head.logoutUrl") + "&token=" + users.getToken();
		ConstatFinalUtil.LOGGER.info("--usersCenterLogoutUrl:{}",usersCenterLogoutUrl);
		/* 客户端跳转 */
		response.sendRedirect(usersCenterLogoutUrl);
		return null ; 
	}
}
